package com.hziee.cosmanage.member.service.impl;

import com.alibaba.fastjson.TypeReference;
import com.hziee.common.utils.R;
import com.hziee.cosmanage.member.entity.*;
import com.hziee.cosmanage.member.feign.CompanyManageFeignService;
import com.hziee.cosmanage.member.service.*;
import com.hziee.cosmanage.member.vo.*;
import com.sun.deploy.panel.CertificatesDialog;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hziee.common.utils.PageUtils;
import com.hziee.common.utils.Query;

import com.hziee.cosmanage.member.dao.InviteDao;


@Service("inviteService")
public class InviteServiceImpl extends ServiceImpl<InviteDao, InviteEntity> implements InviteService {

    @Autowired
    CompanyService companyService;

    @Autowired
    ActivityService activityService;

    @Autowired
    PerormanceService perormanceService;

    @Autowired
    ViolationService violationService;

    @Autowired
    CertificateService certificateService;

    @Autowired
    TrainService trainService;

    @Autowired
    EvaluataeService evaluataeService;

    @Autowired
    ThreadPoolExecutor executor;

    @Autowired
    CompanyManageFeignService companyManageFeignService;

    @Autowired
    UserService userService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<InviteEntity> page = this.page(
                new Query<InviteEntity>().getPage(params),
                new QueryWrapper<InviteEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<InviteEntity> getCompanyInvite(Integer cId) {
        List<InviteEntity> company_id = this.list(new QueryWrapper<InviteEntity>().eq("company_id", cId));
        return company_id;
    }

    @Override
    public boolean upIntroduce(InviteEntity userInviteVo) {
        this.save(userInviteVo);
        return true;
    }

    @Override
    public List<InviteEntity> getIntroduce(Integer cId) {
        List<InviteEntity> list = this.list(new QueryWrapper<InviteEntity>().eq("company_id", cId).eq("status", 1));
        return list;
    }

    @Override
    public UserInfoTalentVo checkUserInfoTalents(Integer uid) throws ExecutionException, InterruptedException {
        UserInfoTalentVo talentVo = new UserInfoTalentVo();
        CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {
            UserEntity byId = userService.getById(uid);
            talentVo.setUsername(byId.getUsername());
        }, executor);
        CompletableFuture<Void> activityFuture = CompletableFuture.runAsync(() -> {
            List<ActivityEntity> activityEntities = activityService.list(new QueryWrapper<ActivityEntity>().eq("user_id", uid));
            talentVo.setActivity(activityEntities);
        },executor);
        CompletableFuture<Void> certificateFuture = CompletableFuture.runAsync(() -> {
            List<CertificateEntity> certificateEntities = certificateService.list(new QueryWrapper<CertificateEntity>().eq("user_id", uid).eq("status",1));
            talentVo.setCertificates(certificateEntities);
        },executor);
        CompletableFuture<Void> evaluateFuture = CompletableFuture.runAsync(() -> {
            List<EvaluataeEntity> evaluataeEntities = evaluataeService.list(new QueryWrapper<EvaluataeEntity>().eq("user_id", uid));
            List<EvaluateVo> collect = evaluataeEntities.stream().map(item -> {
                EvaluateVo evaluateVo = new EvaluateVo();
                String character = "";
                BeanUtils.copyProperties(item, evaluateVo);
                Integer companyId = item.getCompanyId();
                Integer evaluateUserId = item.getEvaluateUserId();
                R companyName = companyManageFeignService.getCompanyName(companyId);
                CompanyVo data = companyName.getData(new TypeReference<CompanyVo>() {
                });
                evaluateVo.setCompany(data.getCompanyName());
                CompanyEntity entity = companyService.getOne(new QueryWrapper<CompanyEntity>().eq("user_id", evaluateUserId).eq("status", 1));
                Integer companyCharacterId = entity.getCompanyCharacterId();
                Integer companyPartId = entity.getCompanyPartId();
                R companyCharacterName = companyManageFeignService.getCompanyCharacterName(companyId, companyCharacterId);
                CharacterVo characterVo = companyCharacterName.getData(new TypeReference<CharacterVo>() {
                });
                String characters = characterVo.getCharacters();

                R companyPartName = companyManageFeignService.getCompanyPartName(companyId, companyPartId);
                PartVo partVo = companyPartName.getData(new TypeReference<PartVo>() {
                });
                String part = partVo.getPart();
                character = part + characters;
                evaluateVo.setEvaluateUserJob(character);
                return evaluateVo;
            }).collect(Collectors.toList());
            talentVo.setEvaluatae(collect);
        }, executor);
        CompletableFuture<Void> performanceFuture = CompletableFuture.runAsync(() -> {
            List<PerormanceEntity> perormanceEntities = perormanceService.list(new QueryWrapper<PerormanceEntity>().eq("user_id", uid));
            talentVo.setPerormance(perormanceEntities);
        }, executor);
        CompletableFuture<Void> VFuture = CompletableFuture.runAsync(() -> {
            List<ViolationEntity> v = violationService.list(new QueryWrapper<ViolationEntity>().eq("user_id", uid));
            talentVo.setViolation(v);
        }, executor);
        CompletableFuture<Void> trainFuture = CompletableFuture.runAsync(() -> {
            List<TrainEntity> trainEntities = trainService.list(new QueryWrapper<TrainEntity>().eq("user_id", uid));
            talentVo.setTrain(trainEntities);
        }, executor);
        CompletableFuture.allOf(future,activityFuture,evaluateFuture,trainFuture,certificateFuture,performanceFuture,VFuture).get();
        return talentVo;
    }


}